//
//  ReadMyDetailViewController.m
//  WYnewsss
//
//  Created by lanou on 16/4/26.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ReadMyDetailViewController.h"
#import "NetWorkrequestManager.h"
#import "FactoryTableViewCell.h"
#import "BaseTableViewCell.h"
#import "ReadMyLastDetailViewcell.h"
#import "ReadMyDetailViewCell.h"
#import "ReadMyDetailModel.h"
#import <MJRefresh.h>
#import "ReadAddInfoViewController.h"
#import "ReadAddDB.h"
#import "UserInfoManager.h"
@interface ReadMyDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *leftTableView;
@property (weak, nonatomic) IBOutlet UITableView *rightTableView;

@property (nonatomic,strong)NSMutableArray *leftArray;
@property (nonatomic,strong)NSMutableArray *rightArray;
@end

@implementation ReadMyDetailViewController
-(NSMutableArray *)leftArray{
    if (!_leftArray) {
        self.leftArray =[NSMutableArray array];
    }
    return _leftArray;
}
-(NSMutableArray *)rightArray{
    if (!_rightArray) {
        self.rightArray=[NSMutableArray array];
    }
    return _rightArray;
}
//http://c.m.163.com/nc/topicset/ios/v4/subscribe/read/recommend.html
-(void)requsetData{
    [NetWorkRequestManager requestWithType:GET urlString:@"http://c.m.163.com/nc/topicset/v5/ios/home.html" parDic:nil finish:^(NSData *data) {
        NSDictionary *dataDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        //NSLog(@"%@",dataDic);
        
        NSArray *array =dataDic[@"iosCategoryList"];
        for (NSMutableDictionary *dic in array) {
            ReadMyDetailModel *leftModel =[[ReadMyDetailModel alloc]init];
            [leftModel setValuesForKeysWithDictionary:dic];
            [self.leftArray addObject:leftModel];
            
            
            //NSLog(@"%@",dic);
            
            
        }
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.leftTableView reloadData];
            
            
            
        });

    } error:^(NSError *error) {
        
    }];
}

//http://c.m.163.com/nc/topicset/v5/ios/C1390559412724/read/all.html
-(void)requestCid:(NSString *)cid{
    NSString *urlStr = nil;
    if (cid==nil) {
        urlStr =@"http://c.m.163.com/nc/topicset/v5/ios/C1378977951794/read/all.html",cid=@"C1378977951794";
        // NSLog(@"%@",cid);
    }else{
        urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/topicset/v5/ios/%@/read/all.html",cid];
        NSLog(@"%@",cid);
    }

    [NetWorkRequestManager requestWithType:GET urlString:urlStr parDic:nil finish:^(NSData *data) {
        NSDictionary *dataDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        //NSLog(@"%@",dataDic);
        
        [self.rightArray removeAllObjects];
        NSArray *rigArray =dataDic[@"tList"];
        for (NSDictionary *dic in rigArray) {
            ReadMyDetailModel *rigModel=[[ReadMyDetailModel alloc]init];
            [rigModel setValuesForKeysWithDictionary:dic];
            [self.rightArray addObject:rigModel];
            //NSLog(@"%@",rigModel);
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.rightTableView reloadData];
            //[self setupRefresh];
            //[self.rightTableView.mj_footer endRefreshing];
        });
        

    } error:^(NSError *error) {
        
    }];
}
//-(void)requestCid:(NSString *)cid{
//   
//    NSString *urlStr = nil;
//    if (cid==nil) {
//        urlStr =@"http://c.m.163.com/nc/topicset/v5/ios/C1378977951794/read/all.html",cid=@"C1378977951794";
//        // NSLog(@"%@",cid);
//    }else{
//       urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/topicset/v5/ios/%@/read/all.html",cid];
//        NSLog(@"%@",cid);
//    }
//   
//    [NetWorkrequestManage requestWithType:GET url:urlStr parameters:nil finish:^(NSData *data) {
//        NSDictionary *dataDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//    //NSLog(@"%@",dataDic);
//        
//        [self.rightArray removeAllObjects];
//        NSArray *rigArray =dataDic[@"tList"];
//        for (NSDictionary *dic in rigArray) {
//            ReadMyDetailModel *rigModel=[[ReadMyDetailModel alloc]init];
//            [rigModel setValuesForKeysWithDictionary:dic];
//            [self.rightArray addObject:rigModel];
//            //NSLog(@"%@",rigModel);
//        }
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [self.rightTableView reloadData];
//            //[self setupRefresh];
//            //[self.rightTableView.mj_footer endRefreshing];
//        });
//        
//        
//    } error:^(NSError *error) {
//        //NSLog(@"%@",error);
//    }];
//}
-(void)requestRem{
    [NetWorkRequestManager requestWithType:GET urlString:@"http://c.m.163.com/nc/topicset/ios/v4/subscribe/read/recommend.html" parDic:nil finish:^(NSData *data) {
        NSDictionary *dataDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [self.rightArray removeAllObjects];
        
        for (NSDictionary *dic in dataDic) {
            ReadMyDetailModel *model=[[ReadMyDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.rightArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.rightTableView reloadData];
        });

    } error:^(NSError *error) {
        
    }];
}
//-(void)requestRem{
//    [NetWorkrequestManage requestWithType:GET url:@"http://c.m.163.com/nc/topicset/ios/v4/subscribe/read/recommend.html" parameters:nil finish:^(NSData *data) {
//        NSDictionary *dataDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        [self.rightArray removeAllObjects];
//       
//            for (NSDictionary *dic in dataDic) {
//                ReadMyDetailModel *model=[[ReadMyDetailModel alloc]init];
//                [model setValuesForKeysWithDictionary:dic];
//                
//                [self.rightArray addObject:model];
//            }
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [self.rightTableView reloadData];
//    });
//        
//            } error:^(NSError *error) {
//        
//    }];
//}
//
//

- (void)setupRefresh
{
    self.rightTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //NSLog(@"----");
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requsetData];
    [self requestRem];
    //
//    UINavigationBar *bar =[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
//    UINavigationItem *item =[[UINavigationItem alloc]initWithTitle:@"订阅推荐更多网易号"];
//    [bar pushNavigationItem:item animated:YES];
//    UIBarButtonItem *leftButon =[[UIBarButtonItem alloc]initWithTitle:@"< 返回" style:(UIBarButtonItemStyleDone) target:self action:@selector(back)];
//    self.leftButton.backgroundColor =[UIColor blackColor];
//    UIBarButtonItem *rightButton =[[UIBarButtonItem alloc]initWithTitle:@"入驻" style:(UIBarButtonItemStyleDone) target:self action:@selector(into)];
//    [item setRightBarButtonItem:rightButton];
//    [item setLeftBarButtonItem:leftButon];
//    [self.view addSubview:bar];
    self.navigationItem.title=@"订阅推荐更多网易号";
    
    [_leftTableView registerNib:[UINib nibWithNibName:@"ReadMyDetailViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([ReadMyDetailModel class])];
    [_rightTableView registerNib:[UINib nibWithNibName:@"ReadMyLastDetailViewcell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([ReadMyDetailModel class])];
    
    
    
    
    
    // Do any additional setup after loading the view from its nib.
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadMyDetailModel *model = self.leftArray[indexPath.row];
    if (tableView==self.leftTableView) {
        if ([model.cName isEqualToString:@"推荐"]){
            [self requestRem];
            [self.leftArray removeObjectAtIndex:1];
        }else{
            [self requestCid:model.cid];
            
        }
    }else {
    ReadAddInfoViewController *infoVC =[[ReadAddInfoViewController alloc]init];
        ReadMyDetailModel *detailModel =_rightArray[indexPath.row];
    //[self presentViewController:infoVC animated:YES completion:nil];
        infoVC.tid = detailModel.tid;
        [self.navigationController pushViewController:infoVC animated:YES];
        

    }
}



    

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView==self.leftTableView) {
        return self.leftArray.count;
    }else{

        return _rightArray.count;
       
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==self.leftTableView) {
        return 40;
    }else{
        return 80;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    ReadMyDetailModel *model=nil;
    if (tableView==self.leftTableView) {
        
        model=self.leftArray[indexPath.row];
       
    }else{
        model=self.rightArray[indexPath.row];
    }
    BaseTableViewCell *cell =[FactoryTableViewCell creatTableViewCell:model withTableView:tableView andIndexPath:indexPath];
    if (tableView==self.leftTableView) {
        cell.textLabel.highlightedTextColor=[UIColor redColor];
        
    }
    [cell setDataWithModel:model];
    
    return cell;
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
